<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./OL_SDK/include-openlayers-local.js"></script>
  <script src="./libs/gaode.js"></script>
  <script src="./utils/setMarkerStyle.js"></script>
  <script src="./utils/createDraw.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0
    }

    #map {
      width: 100vw;
      height: 100vh;
    }

    button#btn {
      width: 200px;
      height: 50px;
      z-index: 1000;
      position: fixed;
      top: 20px;
      left: 120px
    }

    button#clear {
      width: 200px;
      height: 50px;
      z-index: 1000;
      position: fixed;
      top: 20px;
      left: 400px
    }
  </style>
</head>

<body>
  <button id="btn">激活画笔</button>
  <button id="clear" onclick="clearMap()">清除画笔-source</button>
  <div id="map">
  </div>
  <script>
    var map = new ol.Map({
      target: "map",
      layers: [gaode],
      view: new ol.View({
        projection: 'EPSG:4326',
        center: [114.30, 30.50],
        zoom: 4
      })
    })
    /* 设置画笔图层 */
    let marker_style = setMarkerStyle('./images/位置.png')
    let source = new ol.source.Vector({})
    let layer = new ol.layer.Vector({
      source,
      style: marker_style
    })
    map.addLayer(layer)
    var btn = document.getElementById("btn");
    btn.onclick = function () {
      this.disabled = true;
      this.style.cursor = "no-drop"
      /* 激活画笔 */
      let draw = createDraw({
        type: "Point",
        source
      })
      map.addInteraction(draw)

      draw.on("drawend", evt => {
        btn.disabled = false;
        btn.style.cursor = "default"
        // map.removeInteraction(draw);
      })
    }
    function clearMap() {
      btn.disabled = false;
      btn.style.cursor = "default"
      map.getInteractions().pop();
      source.clear()
    }
  </script>
</body>

</html>